const app = getApp();
const api = require('../../api.js');
const util = require('../../utils/util.js');
var WxParse = require('../../component/wxParse/wxParse.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: api.xh.downloadDomain,
    avatar: '/image/course-default-cover.png',
    famous_teacher: [],
    //课程
    courseSelect: '', //
    subjectSelect: '',
    selectGrade: '',
    selectSubject: '',
    show: false,
    selected: 0,
    //视频
    grade_id: '',
    course_id: '',
    knowledg_ids: '',
    selectGradeV: '',
    selectSubjectV: '',
    selectKnowledgeV: '',
    showV: false,
    knowledgeShowV: false,
    selectedV: 0,
    selectedKnowledgeV: 0,
    teacherTagStyle: util.teacherTagStyles,
    isIOS: app.globalData.isIOS
  },
  //  关注 & 取消关注
  toFocus: function(e) {
    var page = this;
    var content_id = e.currentTarget.dataset.id;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth();
    } else {
      app.sendRequest({
        hideLoading: true,
        url: app.api.public.focus,
        data: {
          content_id: content_id,
          type: 2
        },
        method: 'POST',
        header: {
          token: wx.getStorageSync('access_token'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          console.log(page.data.is_focus);
          let is_focus = '';
          is_focus = page.data.is_focus == 1 ? 0 : 1;
          page.setData({
            is_focus: is_focus
          });
        }
      });
    }
  },
  //  教师
  toTeacherDetail: function(e) {
    var teacher_id = e.currentTarget.dataset.id;
    var teacher_title = e.currentTarget.dataset.title;
    var name = e.currentTarget.dataset.name;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth();
    } else {
      wx.navigateTo({
        url:
          '../teacher_detail/teacher_detail?teacher_id=' +
          teacher_id +
          '&teacher_title=' +
          teacher_title +
          '&name=' +
          name
      });
    }
  },
  //html转wxml处理
  processHtml: function() {
    var page = this;
    var famous_teacher = page.data.famous_teacher;
    var profileArr = [];

    famous_teacher.map((teacher, index) => {
      famous_teacher[index].seq = index;
      if (teacher.profile != undefined && teacher.profile != '') {
        profileArr[index] = teacher.profile;
      } else {
        profileArr[index] = '暂无简介';
      }
      WxParse.wxParse('profile' + index, 'html', profileArr[index], page);
      if (index == famous_teacher.length - 1) {
        WxParse.wxParseTemArray(
          'profileTemArray',
          'profile',
          profileArr.length,
          page
        );
      }
    });
    page.setData({
      famous_teacher: famous_teacher
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;
    var school_id = options.school_id;
    let current_user_id = wx.getStorageSync('current_user_id');
    page.setData({
      school_id: school_id
    });

    //  机构介绍
    wx.request({
      url: api.school.info,
      data: {
        school_id: school_id,
        user_id: current_user_id
      },
      success(school_res) {
        if (school_res.data != undefined) {
          let school_info = school_res.data.data;
          let is_focus = school_res.data.data.focus;
          // 设置数据
          page.setData({
            school_info: school_info,
            is_focus: is_focus
          });
          wx.setNavigationBarTitle({
            title: school_info.school_name
          });
        } else {
          page.setData({
            school_info: [],
            is_focus: is_focus
          });
        }
      }
    });

    //  教师
    app.sendRequest({
      url: api.teacher.list,
      data: {
        school_ids: school_id,
        per_page: 9999
      },
      success: function(res) {
        if (res.data.list != '') {
          // 设置数据
          page.setData({
            famous_teacher: res.data.list
          });
          page.processHtml();
        } else {
          page.setData({
            famous_teacher: []
          });
        }
      }
    });

    //  年级学科列表
    app.sendRequest({
      url: api.public.grade_course_student,
      // data: {
      //     type: 1
      // },
      success: function(res) {
        if (res.data != '') {
          // 设置数据
          page.setData({
            course: res.data.list,
            subject: res.data.list[0].course_list
          });
        }
      }
    });
    //  课程列表
    this.getCourseList();

    page.getVideoList();
    //  年级学科列表
    app.sendRequest({
      url: api.public.grade_course_student,
      success: function(res) {
        if (res.data != '') {
          // 设置数据
          page.setData({
            course: res.data.list,
            subject: res.data.list[0].course_list
          });
        }
      }
    });

    if (page.data.course_id === '' && page.data.grade_id === '') {
      page.setData({
        treeData: []
      });
    }
  },

  //  课程列表
  getCourseList: function() {
    var page = this;
    app.sendRequest({
      url: api.course.student_curriculum_list,
      data: {
        school_ids: page.data.school_id,
        grade_id: page.data.courseSelect,
        course_id: page.data.subjectSelect,
        per_page: 9999
      },
      success: function(res) {
        page.setData({ toastHidden: false });
        if (res.data != '' && res.data.list) {
          // 设置数据
          page.setData({
            toastHidden: false,
            hot_courses: res.data.list
          });
        } else {
          page.setData({
            toastHidden: false,
            hot_courses: []
          });
        }
      }
    });
  },

  toggleCourse: function() {
    var page = this;
    page.setData({
      show: !page.data.show
    });
  },

  sureChoose: function() {
    var page = this;
    page.setData({
      show: false
    });
    //  课程列表
    this.getCourseList();
  },

  chooseGrade: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }

    page.setData({
      selectGrade:
        e.currentTarget.dataset.select == ''
          ? '全部'
          : e.currentTarget.dataset.name,
      courseSelect: e.currentTarget.dataset.select
    });
  },

  chooseSubject: function(e) {
    var page = this;
    if (page.data.selected == 0) {
      page.setData({
        selected: 1
      });
    }
    page.setData({
      selectSubject:
        e.currentTarget.dataset.select == ''
          ? ''
          : e.currentTarget.dataset.name,
      subjectSelect: e.currentTarget.dataset.select
    });
  },

  toCourse: function(e) {
    var cid = e.currentTarget.dataset.id;
    var curriculum_name = e.currentTarget.dataset.curriculum_name;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth();
    } else {
      wx.navigateTo({
        url:
          '/pages/course_detail/course_detail?curriculum_id=' +
          cid +
          '&curriculum_name=' +
          curriculum_name
      });
    }
  },
  /* * * ** * * *start 视频* * * ** * * ** * * ** * * ** * * ** * * ** * * */
  getVideoList: function() {
    var page = this;
    page.setData({ toastHidden: false });
    //  视频列表
    wx.request({
      url: api.video.list,
      data: {
        school_id: page.data.school_id,
        grade_id: page.data.grade_id,
        course_id: page.data.course_id,
        knowledg_ids: page.data.knowledg_ids,
        per_page: 9999
      },
      success(res) {
        if (res.data.data !== '' && res.data.data.list) {
          //  处理视频的时长
          for (var index in res.data.data.list) {
            res.data.data.list[index].duration = (
              res.data.data.list[index].duration / 60
            )
              .toFixed(2)
              .replace('.', ':');
          }
          // 设置数据
          page.setData({
            video_list: res.data.data.list
          });
        } else {
          page.setData({
            video_list: []
          });
        }
      }
    });
  },

  toggleCourseV: function() {
    var page = this;
    page.setData({
      showV: !page.data.showV,
      knowledgeShowV: page.data.knowledgeShowV
        ? !page.data.knowledgeShowV
        : page.data.knowledgeShowV
    });
  },

  toggleKnowledgeV: function() {
    var page = this;
    page.setData({
      knowledgeShowV: !page.data.knowledgeShowV,
      showV: page.data.showV ? !page.data.showV : page.data.showV
    });
  },

  sureChooseV: function() {
    var page = this;
    page.setData({
      showV: false,
      treeData: []
    });

    //  知识点
    if (page.data.course_id !== '' && page.data.grade_id !== '') {
      wx.request({
        url: api.public.public_knowledg_list,
        data: {
          grade_id: page.data.grade_id,
          course_id: page.data.course_id
        },
        success(knowledg_res) {
          if (JSON.stringify(knowledg_res.data) != '{}') {
            // 设置数据
            page.setData({
              treeData: knowledg_res.data.data.list
            });
          } else {
            page.setData({
              treeData: [],
              knowledg_ids: '',
              selectKnowledgeV: ''
            });
          }
        }
      });
    } else {
      page.setData({
        treeData: [],
        knowledg_ids: '',
        selectKnowledgeV: ''
      });
    }

    //  视频列表
    this.getVideoList();
  },

  sureChooseVKnowledge: function() {
    var page = this;
    page.setData({
      knowledgeShowV: false
    });
    //  视频列表
    this.getVideoList();
  },

  chooseKnowledgeV: function(e) {
    var page = this;
    if (page.data.selectedKnowledgeV == 0) {
      page.setData({
        selectedKnowledgeV: 1
      });
    }
    page.setData({
      selectKnowledgeV: e.currentTarget.dataset.name,
      knowledg_ids: e.currentTarget.dataset.select
    });
  },

  chooseGradeV: function(e) {
    var page = this;
    if (page.data.selectedV == 0) {
      page.setData({
        selectedV: 1
      });
    }

    page.setData({
      selectGradeV:
        e.currentTarget.dataset.select == ''
          ? '全部'
          : e.currentTarget.dataset.name,
      grade_id: e.currentTarget.dataset.select
    });
  },

  chooseSubjectV: function(e) {
    var page = this;
    if (page.data.selectedV == 0) {
      page.setData({
        selectedV: 1
      });
    }
    page.setData({
      selectSubjectV:
        e.currentTarget.dataset.select == ''
          ? ''
          : e.currentTarget.dataset.name,
      course_id: e.currentTarget.dataset.select
    });
  },

  toVideoDetail: function(e) {
    var video_id = e.currentTarget.dataset.id;
    var logging = wx.getStorageSync('_logining');
    if (!logging) {
      app.toAuth();
    } else {
      wx.navigateTo({
        url: '../video_preview/video_preview?video_id=' + video_id
      });
    }
  },

  /* * * ** * * *end* * * ** * * ** * * ** * * ** * * ** * * ** * * */

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
